﻿using System.Security;
using System.Web;

namespace Cms.Utilities
{
    public enum TrustLevels
    {
        Unrestricted,
        High,
        Medium,
        Low,
        Minimal,
        None
    }

    public class Common
    {
        public static TrustLevels GetCurrentTrustLevel()
        {
            foreach (AspNetHostingPermissionLevel trustLevel in new[]
                                                                    {
                                                                        AspNetHostingPermissionLevel.Unrestricted,
                                                                        AspNetHostingPermissionLevel.High,
                                                                        AspNetHostingPermissionLevel.Medium,
                                                                        AspNetHostingPermissionLevel.Low,
                                                                        AspNetHostingPermissionLevel.Minimal
                                                                    })
            {
                try
                {
                    new AspNetHostingPermission(trustLevel).Demand();
                }
                catch (SecurityException)
                {
                    continue;
                }
                switch (trustLevel)
                {
                    case AspNetHostingPermissionLevel.Unrestricted:
                        return TrustLevels.Unrestricted;
                    case AspNetHostingPermissionLevel.High:
                        return TrustLevels.High;
                    case AspNetHostingPermissionLevel.Medium:
                        return TrustLevels.Medium;
                    case AspNetHostingPermissionLevel.Low:
                        return TrustLevels.Low;
                    case AspNetHostingPermissionLevel.Minimal:
                        return TrustLevels.Minimal;
                }
            }
            return TrustLevels.None;
        }
    }
}